package com.yjh.server.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjh.pojo.entity.CourtInfo;
import com.yjh.pojo.entity.ReservationMain;
import com.yjh.pojo.entity.User;
import com.yjh.pojo.vo.R;
import com.yjh.pojo.vo.ReserveVo;
import com.yjh.server.mapper.CourtInfoMapper;
import com.yjh.server.mapper.ReservationMainMapper;
import com.yjh.server.mapper.UserMapper;
import com.yjh.server.service.ReservationMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ReservationMainServiceImpl extends ServiceImpl<ReservationMainMapper, ReservationMain> implements ReservationMainService {

    @Autowired
    private ReservationMainMapper reservationMainMapper;
    @Autowired
    private CourtInfoMapper courtInfoMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String getMinMaxTime(Integer reserveId){
        Map<String, Object> time = reservationMainMapper.selectMinMaxTimeByReserveId(reserveId);
        Integer minStartHour = (Integer)time.get("min_start_hour");
        Integer maxEndHour = (Integer)time.get("max_end_hour");

        return minStartHour+":00~"+maxEndHour+":00";
    }

    @Override
    public ReserveVo getReservationById(Integer reserveId) {
        ReservationMain reserveMain = reservationMainMapper.selectById(reserveId);

        ReserveVo reserveVo=new ReserveVo();

        User user1 = userMapper.selectById(reserveMain.getUserId());
        CourtInfo courtInfo = courtInfoMapper.selectById(reserveMain.getCourtId());
//        CourtInfo courtInfo = courtInfoService.getById(reserveMain.getCourtId());
//        User user1 = userService.getById(reserveMain.getUserId());
        String minMaxTime = getMinMaxTime(reserveMain.getReserveId());

        reserveVo.setCourtName(courtInfo.getCourtName());
        reserveVo.setReserveDate(reserveMain.getReserveDate());
        reserveVo.setReserveTime(minMaxTime);
        reserveVo.setRealName(user1.getRealname());
        reserveVo.setPhone(user1.getPhone());
        reserveVo.setReserveStatus(reserveMain.getReserveStatus());

        String prefix="RES";
        String randomNum = Integer.toString((int)Math.floor(Math.random() * (999 - 100 + 1)) + 100);
        String checkCode = Integer.toString(reserveId % 7);
        reserveVo.setVerifyId(prefix+reserveId+randomNum+checkCode);


        return reserveVo;
    }
}
